<?php

namespace App\Http\Controllers\Search;

use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Service\Customer\CustomerService;
use App\Service\Search\SearchService;
use Illuminate\Http\Request;



class SearchController extends Controller
{
    public function searchInfo(Request $request)
    {
        $message = $request->message;
        $uid = $request->uid;
        $page = $request->input('page');
        Result::veryShort($message);
		$offest = ($page-1)*10;
		
		$xs = new \XS('xiaofan');
		$search = $xs->search;
		$tokenizer = new \XSTokenizerScws; 
		
		$words = $tokenizer->getResult($message);
		
		$infos = $search->setFuzzy()->setQuery($message)->setLimit(10,$offest)->setSort('reading',true)->search();
        
		if(!$infos){
			Result::msg('-18',"error","没有更多了");
		}
        
        foreach($infos as $key=>$info){
			
			$works[$key]['id'] = $info['pid'];
			$works[$key]['title'] = $info['title'];
			$works[$key]['introduction'] = $info['introduction'];
			$works[$key]['nickname'] = $info['nickname'];
            $works[$key]['headimgurl'] = $info[10];
			$works[$key]['reading'] = $info['reading'];
			$works[$key]['price'] = $info['price'];
            $works[$key]['buy_class'] = $info['buy_class'];
            $works[$key]['buy_num'] = $info['buy_num'];
			$works[$key]['thumb'] = $info['thumb'];
			$works[$key]['create_time'] = $info['create_time'];
		}
        
		Result::msg('0',"success",$works);
        //$page = $page*10;
        /*$works = SearchService::getSearchInfo($uid,$message,10,$offest);
        if($works){
            Result::msg('0',"success",$works);
        }else{
            Result::msg('-18',"error","没有更多了");
        }*/
    }
	
	//搜索类目
    public function searchRecommend(Request $request)
    {
        $data = SearchService::getSearchRecommend();
        if($data){
            Result::msg('0',"success",$data);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }
	
	public function xcxSearchInfo(Request $request)
    {
        $message = $request->message;
        /*if(!CustomerService::findUser(['uid'=>$uid])){
            Result::msg('-16',"error","无有效用户信息");
        }*/
		
        Result::veryShort($message);

        $works = SearchService::getXcxSearchInfo($message);
        if($works){
            Result::msg('0',"success",$works);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }
}